﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using BussinessLogic.Models.Study;
using BussinessLogic.Services;
using System.Web.Security;
using Newtonsoft.Json;
using BussinessLogic.Models;

namespace AspMVCProject.Controllers
{
    [Authorize]
    public class StudyController : Controller
    {
        public ActionResult Index()
        {
            var studies = StudyServices.GetStudiesForPresentation(User.Identity.Name);

            return View(studies);
        }

        [HttpPost]
        public ActionResult ParticipateStudy(int id)
        {
            StudyServices.ParticipateToStudy(User.Identity.Name, id);
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(StudyCreateModel study)
        {
            if (ModelState.IsValid)
            {
                int id = StudyServices.CreateStudy(study);

                return RedirectToAction("Protocol", new { id = id });
            }

            return View(study);
        }

        public ActionResult Delete(int id)
        {
            StudyServices.DeleteStudy(id);
            return RedirectToAction("Maintenance");
        }

        public ActionResult Maintenance()
        {
            var studies = StudyServices.GetStudiesForMaintenance();
            
            return View(studies);
        }

        public ActionResult Edit(int id)
        {
            var study = StudyServices.GetStudy((int)id);

            return View(study);
        }

        [HttpPost]
        public ActionResult Edit(int id, StudyCreateModel model)
        {
            StudyServices.UpdateStudy(id, model);

            return RedirectToAction("Maintenance");
        }

        public ActionResult Protocol(int id)
        {
            StudyProtocol protocol = StudyServices.GetProtocolForStudy(id);
            protocol.StudyID = id;

            if (protocol.Activities.Count == 0)
            {
                ViewBag.Message = "Please create activities first!";
                return View("ActivityInfo");
            }

            if (protocol.Groups == null)
            {
                protocol.Groups = new List<GroupStudy>();

                var group = new GroupStudy { Name = "Group 1", Repeat = 1, };
                group.Sessions = new List<StudySession>();

                var session = new StudySession { Name = "Session 1", Repeat = 1 };
                session.Exercises = new List<StudyExercise>();

                var exercise = new StudyExercise { Name = protocol.Activities.First().Name, ID = protocol.Activities.First().ID, Repeat = 1 };
                session.Exercises.Add(exercise);

                group.Sessions.Add(session);
                protocol.Groups.Add(group);
            }

            return View(protocol);
        }

        [HttpPost]
        public ActionResult Protocol(string jsonResult)
        {
            StudyProtocol protocol = JsonConvert.DeserializeObject<StudyProtocol>(jsonResult);
            StudyServices.SaveProtocol(protocol);

            return RedirectToAction("Maintenance");
        }



        public ActionResult Exercise()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Exercise(IEnumerable<HttpPostedFileBase> files)
        {
            int i = 0;
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var fileExtension = Path.GetExtension(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), "ex1_tr" + i++ + fileExtension);
                    file.SaveAs(path);
                }
            }
            return RedirectToAction("Index");
        }


        [HttpGet]
        public FileResult DownloadExerciseReports(int id)
        {
            string fileName;
            byte[] report = SessionService.GetReportsForExercise(id, out fileName);

            return File(report, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);

        }

        [HttpGet]
        public FileResult DownloadQNReports(int id)
        {
            string fileName;
            byte[] report = SessionService.GetReportsForQN(id, out fileName);

            return File(report, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);

        }
    }
}
